<template>
  <div class="app-container">
    <div class="hint" v-if="disconnect">
      <div style="display:flex">
        断网车站
        <span style="font-size:15px">{{ this.breakStation }}</span
        >个
        <el-button
          class="btn"
          style="background:none"
          v-hasPermi="['station:stationList:edit']"
          @click="getErro"
          >查看</el-button
        >
      </div>

      <div @click="close">
        <el-button class="btn" style="background:none;color:#999"
          >不在提醒</el-button
        >
      </div>
    </div>
    <el-row :gutter="20" style="margin-top:20px">
      <el-col :span="24" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="所属省份" prop="province">
            <el-select
              v-model="queryParams.province"
              clearable
              style="width: '100px'"
              @change="getProvinceName"
            >
              <el-option
                v-for="(item, index) in provincesList"
                :key="index"
                :label="item.province"
                :value="item.province"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属城市" prop="city">
            <el-select
              v-model="queryParams.city"
              clearable
              style="width: '100px'"
            >
              <el-option
                v-for="(item, index) in citys"
                :key="index"
                :label="item.city"
                :value="item.city"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="搜索" prop="shortname">
            <el-autocomplete
              class="inline-input"
              clearable
              v-model="queryParams.shortname"
              :fetch-suggestions="StationQuerySearch"
              placeholder="请输入车站名称搜索"
              size="small"
              :trigger-on-focus="false"
            ></el-autocomplete>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              v-hasPermi="['station:stationList:query']"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              v-hasPermi="['station:stationList:edit']"
              @click="handleAdd"
              >新增</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>
        <!-- 数据表格 -->
        <el-table
          v-loading="loading"
          :data="stationList"
          :max-height="tableHeight"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            width="70"
            type="index"
            label="站点id"
            align="center"
          />
          <el-table-column
            label="车站名称"
            align="center"
            prop="shortname"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="账号"
            align="center"
            prop="nickName"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="所属省份"
            align="center"
            prop="province"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="所属城市" align="center" prop="city" />
          <el-table-column label="区(县)" align="center" prop="area" />
          <el-table-column
            label="详细位置"
            align="center"
            prop="address"
            width="200"
          />
          <el-table-column label="车站编码" align="center" prop="globalcode" />
          <el-table-column
            label="汽车站等级"
            align="center"
            prop="model"
            width="100"
          />
          <el-table-column
            label="负责人电话"
            align="center"
            prop="phonenum,describe"
            width="250"
          >
            <template slot-scope="scope">
              <p v-if="scope.row.describe != null && scope.row.describe != ''">
                {{ scope.row.phonenum }}|{{ scope.row.describe }}
              </p>
              <p v-else>
                {{ scope.row.phonenum }}
              </p>
            </template>
          </el-table-column>
          <el-table-column
            label="咨询电话"
            align="center"
            prop="phones"
            width="250"
          />
          <el-table-column
            label="订票时段"
            align="center"
            prop="starttime,endtime"
            width="120"
          >
            <template slot-scope="scope"
              >{{ scope.row.starttime }}~{{ scope.row.endtime }}</template
            >
          </el-table-column>
          <el-table-column label="是否启用" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.iseticket"
                active-value="1"
                inactive-value="0"
                v-hasPermi="['station:stationList:edit']"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="是否支持在线退票" align="center" width="125">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.isrefund"
                active-value="1"
                inactive-value="0"
                v-hasPermi="['station:stationList:edit']"
                @change="onLineRefund(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="退票规则" align="center" width="550">
            <template slot-scope="scope">
              <p v-if="stationList[scope.$index].before2 == null">以车站为准</p>
              <p v-if="stationList[scope.$index].before2 != null">
                发车两个小时前收取{{ scope.row.before2 }}%发车两个小时以内收取{{
                  scope.row.inner2
                }}% 发车后一个小时以后收取{{ scope.row.after1 }}%
              </p>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                v-hasPermi="['station:stationList:edit']"
                @click="handleUpdate(scope.row)"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageIndex"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-tabs v-model="activeName" type="border-card">
        <el-tab-pane label="车站信息" name="first">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-row>
              <el-col :span="17">
                <el-form-item
                  label="车站编码:"
                  prop="globalcode"
                  label-width="100px"
                >
                  <el-input
                    :disabled="codeStatus"
                    v-model="form.globalcode"
                    placeholder="请输入"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="17">
                <el-form-item
                  label="车站全称:"
                  prop="fullname"
                  label-width="100px"
                >
                  <el-input v-model="form.fullname" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="17">
                <el-form-item
                  label="车站简称:"
                  prop="shortname"
                  label-width="100px"
                >
                  <el-input v-model="form.shortname" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="选择地址:" prop="area" label-width="100px">
                  <el-cascader
                    v-model="site"
                    ref="cascaderAddr"
                    :options="options"
                    clearable
                    @change="getSite"
                  ></el-cascader>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="17">
                <el-form-item
                  label="详细地址:"
                  prop="address"
                  label-width="100px"
                >
                  <el-input
                    v-model="form.address"
                    placeholder="请输入详细地址"
                    maxlength="11"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="17">
                <el-form-item label="接口地址:" label-width="100px">
                  <el-input
                    v-model="form.interfaceurl"
                    placeholder="请输入接口地址"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="17">
                <el-form-item label="接口秘钥:" label-width="100px">
                  <el-input
                    v-model="form.stationpassword"
                    placeholder="请输入接口秘钥"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="17">
                <el-form-item label="接口配置:" label-width="100px">
                  <el-input
                    v-model="form.serveraddress"
                    placeholder="请输入接口配置"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="17">
                <el-form-item label="排序:" label-width="100px">
                  <el-input v-model="form.model" placeholder="请输入排序" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item
                  label="订票时段:"
                  prop="endtime"
                  label-width="100px"
                >
                  <el-time-picker
                    v-model="form.starttime"
                    placeholder="选择开始时间"
                    style="width:200px"
                    format="HH:mm"
                    value-format="HH:mm"
                  ></el-time-picker>
                  <el-time-picker
                    v-model="form.endtime"
                    placeholder="选择截止时间"
                    style="width:200px"
                    format="HH:mm"
                    value-format="HH:mm"
                  ></el-time-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="17">
                <el-form-item
                  label="预售期:"
                  prop="perioddays"
                  label-width="100px"
                >
                  <el-input v-model="form.perioddays" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="17">
                <el-form-item
                  label="负责人"
                  prop="realname"
                  label-width="100px"
                >
                  <el-input v-model="form.realname" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item
                  label="负责人电话:"
                  label-width="100px"
                  prop="phonenum"
                >
                  <el-input v-model="form.phonenum" placeholder="请输入" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="负责人电话2:" label-width="99px">
                  <el-input v-model="form.describe" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item
                  label="咨询电话: "
                  label-width="100px"
                  prop="phones"
                >
                  <el-input v-model="form.phones" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="退票规则" name="second">
          <el-form ref="creat" :model="form" :rules="rules2" label-width="80px">
            <el-row>
              <el-col>
                <el-form-item
                  label="退票规则配置:"
                  label-width="130px"
                  prop="after1"
                >
                  <el-form-item
                    label="发车2小时之前退票,收取手续费:"
                    label-width="220px"
                  >
                    <el-input v-model="form.before2" style="width:50px" />%
                  </el-form-item>
                  <el-form-item
                    label="发车2小时之内退票,收取手续费:"
                    label-width="220px"
                  >
                    <el-input v-model="form.inner2" style="width:50px" />%
                  </el-form-item>
                  <el-form-item
                    label="发车后一小时之内退票,收取手续费:"
                    label-width="240px"
                  >
                    <el-input v-model="form.after1" style="width:50px" />%
                  </el-form-item>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import city from "@/api/station/city";
import utils from "../../js/index";

import {
  getPage,
  Provinces,
  getCitys,
  add,
  modifyFee,
  modify,
  status,
  addContact
} from "@/api/station/stationList";
import { querystations } from "@/api/order/refundList";
import elementIcons from "../../components/icons/element-icons";

export default {
  name: "StationList",
  //   components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      stationList: null,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        globalcode: "",
        province: "",
        shortname: "",
        city: ""
      },
      site: [],
      disconnect: false,
      // 断网车站数量
      breakStation: "",
      //省份列表
      provincesList: [],
      // 城市列表
      citys: [],
      //添加车站弹出层
      open: false,
      //三级联动列表
      options: [],
      // 弹出层标题
      title: "添加车站",
      activeName: "first", //添加切换
      // 所有车站
      stationArry: [],
      //车站编码输入框状态
      codeStatus: false,
      // 表单校验
      rules: {
        globalcode: [
          { required: true, message: "车站编码不能为空", trigger: "blur" }
        ],
        fullname: [
          { required: true, message: "车站全称不能为空", trigger: "blur" }
        ],
        shortname: [
          { required: true, message: "车站简称不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "详细地址不能为空", trigger: "blur" }
        ],
        endtime: [
          { required: true, message: "订票时段不能为空", trigger: "blur" }
        ],
        perioddays: [
          { required: true, message: "预售天数不能为空", trigger: "blur" }
        ],
        area: [{ required: false, message: "地址不能为空", trigger: "blur" }],
        iseticket: [{ required: true, message: "请选择", trigger: "blur" }],
        isrefund: [{ required: true, message: "请选择", trigger: "blur" }],
        realname: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        phonenum: [
          { required: true, message: "负责人电话能为空", trigger: "blur" }
        ],
        phones: [
          { required: true, message: "咨询电话不能为空", trigger: "blur" }
        ]
      },
      rules2: {
        after1: [
          { required: true, message: "退票规则不能为空", trigger: "blur" }
        ]
      }
    };
  },
  watch: {},
  created() {
    this.getList(); //获取车站列表
    this.getProvinces(); //获取省份列表
    this.getStatus(); //获取断网车站数量
    this.getStationList(); //获取所有车站
    this.tableHeight = window.innerHeight - 320; //当前屏幕高度
    this.options = city; //三级联动数据
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        globalcode: undefined,
        fullname: undefined,
        province: undefined,
        city: undefined,
        area: undefined,
        address: undefined,
        interfaceurl: undefined,
        stationpassword: undefined,
        serveraddress: undefined,
        model: undefined,
        starttime: undefined,
        endtime: undefined,
        realname: undefined,
        describe: undefined,
        perioddays: undefined,
        phonenum: undefined,
        phones: undefined,
        before2: undefined,
        inner2: undefined,
        after1: undefined,
        id: undefined,
        realname: undefined,
        stationname: undefined
      };
      // this.resetForm("form");
    },
    /** 查询车站列表 */
    getList() {
      getPage(this.queryParams).then(response => {
        if (response.code == 200) {
          this.loading = false;
          this.stationList = response.rows;
          this.total = response.total;
        } else {
          this.$message.error(response.msg);
        }
      });
    },
    // 查询断网车站信息
    getStatus() {
      status().then(response => {
        this.breakStation = response.data.error;
        if (this.breakStation != 0) {
          this.disconnect = true;
        }
      });
    },
    // 查看按钮
    getErro() {
      // 跳转到详情页面
      this.$router.push({
        path: "/operationLog/stationErro"
      });
    },
    //不再提醒按钮
    close() {
      this.disconnect = false;
    },
    //查询归属省列表
    getProvinces() {
      Provinces().then(response => {
        this.provincesList = response.data;
      });
    },
    // 获取选中的省的名字
    getProvinceName(val) {
      // 获取省下的市列表
      getCitys({ province: val }).then(response => {
        this.citys = response.data;
      });
    },
    // 车站名模糊查询
    StationQuerySearch(queryString, cb) {
      var restaurantsStation = this.stationArry;
      var results = queryString
        ? restaurantsStation.filter(this.StationCreateFilter(queryString))
        : restaurantsStation;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    StationCreateFilter(queryString) {
      return v => {
        return v.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0;
      };
    },
    // 获取所有车站名
    getStationList() {
      querystations().then(res => {
        if (res.code == 200) {
          var arr = [];
          res.data.forEach(item => {
            arr.push({ value: item.stationName });
          });
          this.stationArry = arr;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    createFilter(queryString) {
      return restaurant => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0
        );
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 新增里面获取地址参数
    getSite() {
      const checkedNodes = this.$refs["cascaderAddr"].getCheckedNodes(); //获取当前点击的节点
      this.form.province = checkedNodes[0].pathLabels[0]; //省
      this.form.city = checkedNodes[0].pathLabels[1]; //市
      this.form.area = checkedNodes[0].pathLabels[2]; //区/县
    },
    // 电子客票状态修改
    handleStatusChange(row) {
      modify({
        globalcode: row.globalcode,
        iseticket: Number(row.iseticket)
      }).then(response => {
        if ((response.code = 200)) {
        }
      });
    },
    // 是否支持在线退票状态修改
    onLineRefund(row) {
      modify({
        globalcode: row.globalcode,
        isrefund: row.isrefund
      }).then(response => {});
    },
    //编辑
    handleUpdate(row) {
      this.reset();
      this.codeStatus = true;
      this.title = "编辑车站";
      this.open = true;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
      // 详情赋值
      this.form = row;
      this.form.province = row.province;
      this.form.city = row.city;
      this.form.area = row.area;
      this.form.realname = row.realname;
      this.form.phonenum = row.phonenum;
      this.form.describe = row.describe;
      this.form.id = row.id;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {},
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.codeStatus = false;
      this.site = [];
      this.open = true;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.stationname = this.form.shortname;
          if (
            this.form.before2 != null &&
            this.form.inner2 != null &&
            this.form.after1 != null
          ) {
            add(this.form).then(response => {
              if (response.code == 200) {
              } else {
                this.$message.error(response.msg);
              }
              // this.getList();
            });
            addContact(this.form).then(response => {
              if (response.code == 200) {
              } else {
                this.$message.error(response.msg);
              }
            });
            modifyFee(this.form).then(response => {
              if (response.code == 200) {
                this.open = false;
                this.getList();
              } else {
                this.$message.error(response.msg);
              }
            });
          } else {
            this.$message({
              showClose: true,
              message: "请输入退票规则",
              type: "warning"
            });
            // return;
          }
        }
      });
    }
  }
};
</script>
<style>
.el-cascader--medium {
  width: 400px;
}
.hint {
  width: 80%;
  height: 30px;
  padding: 0 10px;
  border: 1px solid red;
  line-height: 30px;
  font-size: 13px;
  color: #999;
  border-radius: 5px;
  display: flex;
  justify-content: space-between;
  background-color: #ffe4e1;
}
.btn {
  background: none;
  border: none;
  height: 0px;
  color: blue;
  margin-top: -2px;
  margin-left: -15px;
}
</style>
